import { createStore } from "vuex";
import { ref } from "vue"
import user from "../Api/user.js";

import { addressList } from './../compents/piaoyi-cityPicker/cityData';
// import store from './index';
const store = createStore({
  state: () => ({
    code: -11,
    //地址下标
    addindex: 0,
    token: '',
    //验证码
    Verificationcode: 0,
    //地址列表
    addressList: [{
      title: "北京市市辖区东城区璃月",
      text: "巴尔泽布",
      phone: "133323356562"
    }],
    //轮播图
    bannerlist: [],
    //金刚区
    Konglist: [],
    //商品列表
    goodslist: [],
    //修改地址原地址
    addresschange: [],
    //购物车列表
    cartlist: []
  }),
  mutations: {
    //获取地址列表
    addressList(state, payload) {
      state.addressList = payload
    },
    // 添加地址
    append(state, payload) {
      state.addressList.push(payload)
    },
    //点击地址获取地址列表的下表
    changeaddindex(state, payload) {
      state.addindex = payload
    },
    // 更改验证码的值
    Verificationcode(state, payload) {
      state.Verificationcode = payload
    },
    //更改token的值
    token(state, payload) {
      state.token = payload
    },
    //更改code的值
    code(state, payload) {
      state.code = payload
    },
    //更改轮播图的值
    bannerlist(state, payload) {
      state.bannerlist = payload
    },
    //更改金刚区的值
    Konglist(state, payload) {
      state.Konglist = payload
    },
    //更改商品列表的值
    goodslist(state, payload) {
      state.goodslist = payload
    },
    //修改地址原地址
    addresschange_id(state, payload) {
      state.addresschange = payload
    },
    //***************************************************************************** */
    //购物车列表
    //*************************************************************************** */
    cartlist(state, payload) {
      state.cartlist = payload
    },

  },
  getters: {
    // 获取地址列表中的一个
    addindexlist(state) {
      return state.addressList[state.addindex]
    }
  },
  actions: {
    // 验证码
    loginVerificationcode(state, payload) {
      return user.login({ mobile: payload }).then((res) => {
        state.commit("Verificationcode", res.data.data.code)
        console.log(res);
      });
    },
    // 验证码登录
    loginverify(state, payload) {
      console.log(payload);
      return user.loginverify({
        mobile: payload.phone,
        code: payload.yanzhengmanum
      }).then((res) => {
        state.commit("token", res.data.data.token.token)
        console.log(res);
      })
    },
    //密码登录
    loginpassword(state, payload) {
      return user.loginpassword({
        mobile: payload.phone,
        password: payload.password
      }).then((res) => {
        state.commit("token", res.data.data.token.token)
        console.log(res);

      })
    },
    //密码注册账号 
    register(state, payload) {
      return user.register({
        mobile: payload.phone,
        code: payload.yanzhengmanum,
        password: payload.password,
        passwords: payload.passwords
      }).then((res) => {
        console.log(res.data.code);
        console.log(res.data.msg);
        state.commit("code", res.data.code)
      })
    },
    //***************************************************************************** */
    //index页面接口
    //*************************************************************************** */
    // 轮播图
    banner(state) {
      return user.banner().then((res) => {
        state.commit("bannerlist", res.data.data)
      })
    },
    //金刚区
    Kong(state) {
      return user.Kong().then((res) => {
        // console.log(res);
        state.commit("Konglist", res.data.data)
      })
    },
    //商品列表
    goodslist(state) {
      return user.goodslist().then((res) => {
        // console.log(res);
        state.commit("goodslist", res.data.data)
      })
    },
    //地址管理--获取地址列表
    onLoadAddressList(state) {
      return user.addresslist().then((res) => {
        state.commit("addressList", res.data.data)
        console.log(res);
      })
    },
    //地址管理--添加地址
    addressUpdata(state, payload) {
      // console.log(payload);
      return user.addressUpdata({
        name: payload.name,
        phone: payload.phone,
        city: payload.city,
        address: payload.address,
        status: payload.status
      }).then((res) => {
        console.log(res);
      })
    },
    //地址管理--删除地址
    addressDelete(state, payload) {
      return user.addressDelete({
        id: payload
      }).then((res) => {
        console.log(res);
      })
    },
    //地址管理--修改地址--获取原地址
    addresschange_id(state, payload) {
      return user.addresschange_id({
        id: payload
      }).then((res) => {
        // console.log(res);
        state.commit("addresschange_id", res.data.data)
      })
    },
    //地址管理--修改地址--上传地址
    addresschange(state, payload) {
      return user.addresschange({
        id: payload.id,
        name: payload.name,
        phone: payload.phone,
        city: payload.city,
        address: payload.address,
      }).then((res) => {
        console.log(res);
        console.log(123);
      })
    },
    //购物车管理--获取购物车列表
    cartlist(state) {
      return user.cartlist().then((res) => {
        store.commit("cartlist", res.data.data)
      })
    },

  }


});
export default store;
